import { _decorator, Component, instantiate, Node, Material, Prefab, Texture2D } from 'cc';
import { SkinManager } from "./SkinManager";
const { ccclass, property } = _decorator;

@ccclass('ResourceManager') // 用 @ccclass 声明类为 Cocos 组件
export class ResourceManager extends Component {

    @property(Material)  desktopMat!: Material;

    @property([Texture2D])  cardTexture: Texture2D[] = [];

    @property(Material)  baseCardMat!: Material;
    @property(Material)  handCardMat: Material | null = null;
    @property(Material)  touchCardMat: Material | null = null;
    @property(Material)  kingCardMat!: Material;
    @property(Material)  selectCardMat!: Material;

    @property(Material)  blackCardMat!: Material;
    @property(Material)  disCardMat!: Material;

    @property([Prefab])  cards1: Prefab[] = [];
    @property([Prefab])  cards2: Prefab[] = [];
    @property([Prefab])  cards3: Prefab[] = [];
    @property([Prefab])  cards4: Prefab[] = [];

    @property([Material])  windMat: Material[] = [];

	static instance: ResourceManager;

    onLoad() {
        console.log("ResourceManager onLoad~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        ResourceManager.instance = this;
    }

    static getDesktopMat() {
        return ResourceManager.instance.desktopMat;
    }

    static getCardTexture(index : number) {
        return ResourceManager.instance.cardTexture[index];
    }

    static getBaseCardMat() {
        return ResourceManager.instance.baseCardMat;
    }

    static getHandCardMat() {
        return ResourceManager.instance.handCardMat;
    }

    static getTouchCardMat() {
        return ResourceManager.instance.touchCardMat;
    }

    static getKingCardMat() {
        return ResourceManager.instance.kingCardMat;
    }

    static getSelectdCardMat() {
        return ResourceManager.instance.selectCardMat;
    }

    static getBlackCardMat() {
        return ResourceManager.instance.blackCardMat;
    }

    static getDisCardMat() {
        return ResourceManager.instance.disCardMat;
    }

    static getWinddMat(enable: boolean) {
        if(enable)
        {
            return ResourceManager.instance.windMat[1];
        }
        else
        {
            return ResourceManager.instance.windMat[0];
        }
        
    }

    static getNewCard(cardID: number) {
        const type = Math.floor(cardID / 100);
        const value = Math.floor(cardID / 10) % 10;
        let resArray;
        switch(type)
        {
            case 1:
                resArray = ResourceManager.instance.cards1;
                break;
            case 2:
                resArray = ResourceManager.instance.cards2;
                break;
            case 3:
                resArray = ResourceManager.instance.cards3;
                break;
            case 4:
                resArray = ResourceManager.instance.cards4;
                break;
            default:
                resArray = ResourceManager.instance.cards1;
                break;
        }

        if(resArray[value - 1])
        {
            const newCard = instantiate(resArray[value - 1]);
            return newCard;
        }
        return null;
    }
}